package com.ruoyi.gp.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gp.domain.vo.AuditVo;
import com.ruoyi.gp.domain.bo.AuditBo;
import com.ruoyi.gp.service.IAuditService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 审核
 *
 * @author ruoyi
 * @date 2024-03-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/audit")
public class AuditController extends BaseController {

    private final IAuditService iAuditService;

    /**
     * 查询审核列表
     */
    @SaCheckPermission("system:audit:list")
    @GetMapping("/list")
    public TableDataInfo<AuditVo> list(AuditBo bo, PageQuery pageQuery) {
        return iAuditService.queryPageList(bo, pageQuery);
    }
    /**
     * 查询审核列表
     */
    @SaCheckPermission("system:audit:doctor")
    @GetMapping("/doctor")
    public TableDataInfo<AuditVo> list(long doctorId, String status,PageQuery pageQuery) {
        return iAuditService.queryByDoctorId(doctorId,status, pageQuery);
    }


    /**
     * 导出审核列表
     */
    @SaCheckPermission("system:audit:export")
    @Log(title = "审核", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AuditBo bo, HttpServletResponse response) {
        List<AuditVo> list = iAuditService.queryList(bo);
        ExcelUtil.exportExcel(list, "审核", AuditVo.class, response);
    }

    /**
     * 获取审核详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:audit:query")
    @GetMapping("/{id}")
    public R<AuditVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iAuditService.queryById(id));
    }

    /**
     * 新增审核
     */
    @SaCheckPermission("system:audit:add")
    @Log(title = "审核", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AuditBo bo) {
        return toAjax(iAuditService.insertByBo(bo));
    }

    /**
     * 修改审核
     */
    @SaCheckPermission("system:audit:edit")
    @Log(title = "审核", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AuditBo bo) {
        return toAjax(iAuditService.updateByBo(bo));
    }

    /**
     * 删除审核
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:audit:remove")
    @Log(title = "审核", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iAuditService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
